<template>
  <div>
    <avue-crud
      :option="optionCustomerbill"
      :table-loading="loading"
      :data="data"
      :page="page"
      @row-del="rowDel"
      v-model="form"
      @row-update="rowUpdate"
      @row-save="rowSave"
      :before-open="beforeOpen"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @on-load="onLoad"
    >
      <template #menu="{ row, index, size }">
        <el-button
          type="text"
          icon="el-icon-view"
          @click="openCustomerBillDetail(row)"
          >查看</el-button
        >
        <el-button
          type="text"
          icon="el-icon-document-checked"
          @click="isSetting(row)"
          >结算</el-button
        >
      </template>
      <template #menuLeft>
        <div class="flex-center m-t-20 m-b-10">
          <el-date-picker
            size="small"
            v-model="searchDate"
            type="daterange"
            class="m-r-10"
            value-format="yyyy-MM-dd HH:mm:ss"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
          <el-select
            style="display: block"
            class="m-r-10"
            size="small"
            v-model="query.isSealed"
            placeholder="结算"
            clearable
            @change="changeValue"
            @clear="changeValue"
          >
            <el-option
              style="width: 100px"
              v-for="item in optionIsSettled"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-input
            size="small"
            clearable
            v-model="query.userName"
            class="m-r-10"
            placeholder="请输入账单号或客户名称"
          />
          <el-button
            size="small"
            icon="el-icon-search"
            type="primary"
            @click="conditionalQuery"
            :loading="loading"
            >查询</el-button
          >
        </div>
      </template>
    </avue-crud>
    <el-dialog
      title="账单结算"
      append-to-body
      :visible.sync="showSettingFlag"
      width="400px"
    >
      <div class="pa-12 flex-start">
        <div style="width: 100px">结算金额</div>
      </div>
      <div class="pa-12 flex-start">
        <div style="width: 100px">备注</div>
        <el-input type="textarea"></el-input>
      </div>
      <div class="dividing-line m-t-20 m-b-20"></div>
      <div class="flex-end">
        <el-button type="primary" size="small" @click="doSetting"
          >确定</el-button
        >
        <el-button
          type="primary"
          size="small"
          plain
          @click="showSettingFlag = false"
          >取消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { isListEmpty } from "@/util/common-util";
import { getList } from "@/api/financial/customer-bill";
import { optionCustomerbill } from "@/const/financial/customer-bill";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      optionCustomerbill,
      showSettingFlag: false,
      data: [],
      searchDate: null,
      optionIsSettled: [
        {
          label: "全部",
          value: undefined,
        },
        {
          label: "未结算",
          value: 0,
        },
        {
          label: "已结算",
          value: 1,
        },
      ],
      viewDialog: false,
    };
  },
  computed: {
    ...mapGetters(["permission"]),
  },
  methods: {
    openCustomerBillDetail(row) {
      this.$emit("openCustomerBillDetail", row);
    },
    //账单结算
    isSetting(row) {
      this.showSettingFlag = true;
    },
    //搜索查询
    conditionalQuery() {
      if (isListEmpty(this.searchDate)) {
        this.query.startTime = "";
        this.query.endTime = "";
      } else {
        this.query.startTime = this.searchDate[0];
        this.query.endTime = this.searchDate[1];
      }
      this.onLoad(this.page, this.query);
    },
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowUpdate(row, index, done, loading) {
      update(row).then(
        () => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        },
        (error) => {
          window.console.log(error);
          loading();
        }
      );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        getDetail(this.form.id).then((res) => {
          this.form = res.data.data;
          this.viewDialog = true;
        });
      }
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
      });
    },
  },
};
</script>

<!--<style scoped>-->
<!--</style>-->
